package com.application.lucky.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.activity.PbChatActivity;
import com.application.lucky.activity.WebViewActivity;
import com.application.lucky.adapter.WebBannerAdapter;
import com.application.lucky.adapter.baseadapter.ViewPagerFragmentAdapter;
import com.application.lucky.config.Config;
import com.application.lucky.fragment.group.FragmentChatRoom;
import com.application.lucky.fragment.group.FragmentGroupGame;
import com.application.lucky.fragment.group.FragmentGroupMessage;
import com.application.lucky.widget.banner.BannerLayout;
import com.application.mylibrary.base.BaseFragment;
import com.qunar.im.base.common.ConversitionType;

import java.util.ArrayList;

public class FragmentGroup extends BaseFragment {

    private TabLayout tabView;
    private BannerLayout bannerView;
    private ViewPager vpView;

    @Override
    protected void beforeInit(Bundle arguments) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_group_layout;
    }

    @Override
    public void initView(View view) {
        tabView = view.findViewById(R.id.tab_view);
        vpView = view.findViewById(R.id.vp_view);
        bannerView = view.findViewById(R.id.banner_view);


//        List<> list = new ArrayList<>();
        int res[]={R.drawable.icon_fragment_activity_img,R.drawable.icon_fragment_activity_img,R.drawable.icon_fragment_activity_img,R.drawable.icon_fragment_activity_img};
//        list.add("http://img0.imgtn.bdimg.com/it/u=1352823040,1166166164&fm=27&gp=0.jpg");
//        list.add("http://img3.imgtn.bdimg.com/it/u=2293177440,3125900197&fm=27&gp=0.jpg");
//        list.add("http://img3.imgtn.bdimg.com/it/u=3967183915,4078698000&fm=27&gp=0.jpg");
//        list.add("http://img0.imgtn.bdimg.com/it/u=3184221534,2238244948&fm=27&gp=0.jpg");
//        list.add("http://img4.imgtn.bdimg.com/it/u=1794621527,1964098559&fm=27&gp=0.jpg");
//        list.add("http://img4.imgtn.bdimg.com/it/u=1243617734,335916716&fm=27&gp=0.jpg");
        WebBannerAdapter webBannerAdapter = new WebBannerAdapter(mCOntext, res);
        webBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                WebViewActivity.jump(mCOntext, "https://www.baidu.com");
            }
        });
        bannerView.setAdapter(webBannerAdapter);

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new FragmentGroupMessage());
        fragments.add(new FragmentChatRoom());
        fragments.add(new FragmentGroupGame());
        vpView.setAdapter(new ViewPagerFragmentAdapter(getChildFragmentManager(), fragments));

//        tabView.setupWithViewPager(vpView);
        vpView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int pos) {
                TabLayout.Tab tabAt = tabView.getTabAt(pos);
                if (tabAt != null) {
                    tabAt.select();
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        tabView.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 3) {
                    Intent intent = new Intent(getContext(), PbChatActivity.class);

                    intent.putExtra(PbChatActivity.KEY_JID, Config.IMIdUtils.getIMUid(Config.KEFU_ID));
                    intent.putExtra(PbChatActivity.KEY_CHAT_TYPE, String.valueOf(ConversitionType.MSG_TYPE_CHAT));
                    intent.putExtra(PbChatActivity.KEY_IS_CHATROOM, false);
                    startActivity(intent);
//                    startActivity(new Intent(mCOntext, KeFuActivity.class));

                } else {
                    vpView.setCurrentItem(tab.getPosition());
                }

                changeTabSelect(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabSelect(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                if (tab.getPosition() == 3) {
                    Intent intent = new Intent(getContext(), PbChatActivity.class);

                    intent.putExtra(PbChatActivity.KEY_JID, Config.IMIdUtils.getIMUid(Config.KEFU_ID));
                    intent.putExtra(PbChatActivity.KEY_CHAT_TYPE, String.valueOf(ConversitionType.MSG_TYPE_CHAT));
                    intent.putExtra(PbChatActivity.KEY_IS_CHATROOM, false);
                    startActivity(intent);
//                    startActivity(new Intent(mCOntext, KeFuActivity.class));
                }
            }
        });
        tabView.getTabAt(0).setCustomView(getView("红包游戏",R.drawable.icon_group_hongbao));
        tabView.getTabAt(1).setCustomView(getView("聊天室",R.drawable.icon_group_chatroom));
        tabView.getTabAt(2).setCustomView(getView("发现好玩",R.drawable.icon_group_game));
        tabView.getTabAt(3).setCustomView(getView("在线客服",R.drawable.icon_group_kefu));
        changeTabSelect(tabView.getTabAt(0), true);
    }

    //创建Tablayout的CustomView
    private View getView(String title, int icon) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_group_tab, null);
        TextView tv = view.findViewById(R.id.tv);
        ImageView iv = view.findViewById(R.id.iv);
        tv.setText(title);
        iv.setImageResource(icon);
        return view;
    }

    private void changeTabSelect(TabLayout.Tab tab, boolean select) {
        View view = tab.getCustomView();
        TextView txt_title = (TextView) view.findViewById(R.id.tv);
        txt_title.setTextColor(select ? getResources().getColor(R.color.doMain_text) : getResources().getColor(R.color.black));
    }
}
